var t, e = require("../../@babel/runtime/helpers/interopRequireDefault"), a = require("../../@babel/runtime/helpers/toConsumableArray"), i = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), o = require("../../@babel/runtime/helpers/defineProperty"), s = require("../../utils/tool"), n = e(require("../../http/carApi")), d = e(require("../..//http/address")), c = getApp(), r = (require("../../http/productApi.js"), 
new n.default()), u = new d.default(), l = require("../../http/indexApi.js");

Page({
    data: (t = {
        systemInfo: c.data.systemInfo,
        safeAreaHeight: c.global.systemInfo.safeAreaHeight,
        productList: [],
        hasMore: !0,
        loadingMore: !1,
        deleteType: 0,
        validList: [],
        invalidList: [],
        discountType: 0,
        couponHistoryId: "",
        discountPrice: 0,
        isAll: !1
    }, o(t, "discountType", 0), o(t, "addressId", ""), o(t, "products", []), o(t, "couponCount", ""), 
    o(t, "integralTotal", 0), o(t, "integralUsable", 0), o(t, "integralToMoney", 0), 
    o(t, "integralStatus", null), o(t, "couponList", []), o(t, "title", ""), o(t, "loading", !1), 
    o(t, "couponToMoney", ""), t),
    onLoad: function() {
        this.getCarList();
    },
    onShow: function() {
        this.selectComponent("#deliver").close(), this.hideSuccess();
    },
    changeAll: function(t) {
        var e = t.detail.select;
        if (0 != this.data.validList.length) {
            var a, o = [], n = this.data.validList, d = i(n);
            try {
                for (d.s(); !(a = d.n()).done; ) {
                    var c = a.value;
                    o.push(c.productId), c.select = e;
                }
            } catch (t) {
                d.e(t);
            } finally {
                d.f();
            }
            e ? this.setData({
                isAll: e,
                products: o,
                validList: n,
                discountType: 0,
                couponHistoryId: ""
            }) : this.setData({
                isAll: e,
                products: [],
                validList: n,
                discountType: 0,
                couponHistoryId: ""
            }), this.getPrice(), this.setTitle();
        } else (0, s.toastToPage)("暂无可选商品");
    },
    getId: function(t) {
        var e = t.detail.productId, i = t.detail.select, o = new Set(this.data.products);
        i ? o.add(e) : o.delete(e);
        var s = this.data.validList, n = s.findIndex(function(t) {
            return t.productId == e;
        });
        s[n].select = i, this.setData({
            products: a(o),
            isAll: o.size > 0 && o.size == this.data.validList.length,
            validList: s,
            discountType: 0,
            couponHistoryId: ""
        }), this.getPrice(), this.setTitle();
    },
    setTitle: function() {
        var t = this, e = 0;
        if (this.data.products.length > 0) {
            var a, o = i(this.data.products);
            try {
                var s = function() {
                    var i = a.value, o = t.data.validList.findIndex(function(t) {
                        return t.productId == i;
                    });
                    e += t.data.validList[o].quantity;
                };
                for (o.s(); !(a = o.n()).done; ) s();
            } catch (t) {
                o.e(t);
            } finally {
                o.f();
            }
            this.setData({
                title: "购物车(选中".concat(e, "件)")
            });
        } else {
            var n, d = i(this.data.validList);
            try {
                for (d.s(); !(n = d.n()).done; ) {
                    var c = n.value;
                    e += c.quantity;
                }
            } catch (t) {
                d.e(t);
            } finally {
                d.f();
            }
            var r, u = i(this.data.invalidList);
            try {
                for (u.s(); !(r = u.n()).done; ) {
                    var l = r.value;
                    e += l.quantity;
                }
            } catch (t) {
                u.e(t);
            } finally {
                u.f();
            }
            this.setData({
                title: "购物车(共".concat(e, "件)")
            });
        }
    },
    getPrice: function() {
        var t = this;
        r.carPrice({
            products: this.data.products,
            discountType: this.data.discountType,
            couponHistoryId: this.data.couponHistoryId
        }).then(function(e) {
            t.setData({
                couponCount: e.data.couponCount,
                integralTotal: e.data.integration,
                integralUsable: e.data.sureIntegrationNum,
                integralToMoney: e.data.sureIntegrationMoney,
                couponToMoney: e.data.discountToAmount,
                discountPrice: e.data.discountPrice ? (1 * e.data.discountPrice).toFixed(2) : e.data.discountPrice,
                price: (1 * e.data.discountPrice).toFixed(2).split("."),
                originalPrice: (1 * e.data.sumOnlyProduct).toFixed(2)
            });
        }).catch(function(t) {});
    },
    showClearDialog: function(t) {
        0 != this.data.invalidList.length ? (this.setData({
            deleteType: t.currentTarget.dataset.type
        }), this.selectComponent("#clear-dialog").show()) : (0, s.toastToPage)("无可清空商品");
    },
    getCarList: function() {
        var t = this;
        wx.showLoading({
            title: "加载中",
            mask: !0
        }), this.setData({
            loading: !0
        }), r.carList().then(function(e) {
            var a, o = i(e.data.valid);
            try {
                for (o.s(); !(a = o.n()).done; ) {
                    a.value.select = !1;
                }
            } catch (t) {
                o.e(t);
            } finally {
                o.f();
            }
            t.setData({
                validList: e.data.valid,
                invalidList: e.data.invalid,
                loading: !1,
                products: [],
                couponHistoryId: "",
                discountType: 0,
                isAll: !1
            }), t.setTitle(), t.getPrice(), wx.hideLoading();
        }).catch(function(e) {
            t.setData({
                loading: !1
            }), wx.hideLoading();
        });
    },
    getDefaultAddressId: function() {
        var t = this;
        0 != this.data.products.length ? (wx.showLoading({
            mask: !0,
            title: "加载中…"
        }), u.getDefaultAddress().then(function(e) {
            t.setData({
                addressId: e.data.addressId || ""
            }), t.buy();
        }).catch(function(t) {
            (0, s.toastToPage)(t.msg), wx.hideLoading();
        })) : (0, s.toastToPage)("请至少选择一件商品");
    },
    buy: function() {
        var t = this;
        if (!this.data.addressId) return wx.hideLoading(), void this.selectComponent("#deliver").showPopup();
        r.carOrder({
            discountType: this.data.discountType,
            couponHistoryId: this.data.couponHistoryId,
            addressId: this.data.addressId,
            products: this.data.products
        }).then(function(e) {
            t.orderId = e.data.orderId, "0.00" == t.data.discountPrice ? t.carZeroBuy(e.data.orderId) : t.getCode(e.data.orderId);
        }).catch(function(t) {
            (0, s.toastToPage)(t.msg), wx.hideLoading();
        });
    },
    carZeroBuy: function(t) {
        var e = this;
        r.zeroBuy(t).then(function(t) {
            e.successNews(), wx.hideLoading();
        }).catch(function(t) {
            wx.hideLoading();
        });
    },
    getCode: function(t) {
        var e = this;
        wx.login({
            success: function(a) {
                a.code ? e.getPayInfo(a.code, t) : ((0, s.toastToPage)("获取code失败" + a.errMsg), wx.hideLoading());
            }
        });
    },
    getPayInfo: function(t, e) {
        var a = this;
        r.payInfo(t, e).then(function(t) {
            (0, s.callWeChatPay)(t.data.miniPayRequest, function(t) {
                a.successNews(), wx.hideLoading();
            }, function(t) {
                a.cancelOrder(e);
            });
        }).catch(function(t) {
            (0, s.toastToPage)(t.msg), wx.hideLoading();
        });
    },
    cancelOrder: function(t) {
        r.cancelCarOrder(t).then(function(t) {
            wx.hideLoading(), (0, s.toastToPage)("订单已取消");
        }).catch(function(t) {
            (0, s.toastToPage)(t.msg), wx.hideLoading();
        });
    },
    clear: function() {
        var t = this;
        r.clearCar(this.data.deleteType).then(function(e) {
            t.selectComponent("#clear-dialog").onClose(), (0, s.toastToPage)(e.msg), t.getCarList();
        }).catch(function(e) {
            t.selectComponent("#clear-dialog").onClose(), (0, s.toastToPage)(e.msg);
        });
    },
    successNews: function() {
        var t = this, e = this;
        wx.requestSubscribeMessage({
            tmplIds: [ "vBPr22DDXOhYMu7o3hc3k055YKUvYSmncOQjkcVzLE0", "hdkwX1PFKY08Wrw3z18S6oFpn2SeoyToVW4qGW2L-4w", "C1t5mpxs6FRapLx2Ht1gnmwpDyKwJj1HkEzWJ_mv6R0" ],
            success: function(t) {
                console.log(t), l.subscribeNotice({
                    messageType: 7,
                    messageId: e.orderId
                }).then(function(t) {}).catch(function(t) {});
            },
            fail: function(t) {
                wx.hideLoading();
            },
            complete: function(e) {
                t.showSuccess();
            }
        });
    },
    showSuccess: function() {
        this.selectComponent("#success").showPopup();
    },
    hideSuccess: function() {
        this.selectComponent("#success").data.show && this.selectComponent("#success").onClose();
    },
    tappingTicket: function() {
        0 != this.data.products.length ? this.data.couponCount <= 0 || this.getCouponList() : (0, 
        s.toastToPage)("请先选择商品");
    },
    tappingIntegral: function() {
        0 != this.data.products.length ? this.data.integralUsable <= 0 || this.selectComponent("#integral").showPopup() : (0, 
        s.toastToPage)("请先选择商品");
    },
    getCouponList: function() {
        var t = this;
        wx.showLoading({
            title: "加载中",
            mask: !0
        }), r.carTicket({
            products: this.data.products
        }).then(function(e) {
            var a, o = i(e.data.canUseCouponList);
            try {
                for (o.s(); !(a = o.n()).done; ) {
                    var n = a.value;
                    n.isUsable = (0, s.isLessThanNow)(n.collectStartTime), n.isExpire = (0, s.getDifferentDays)(n.collectEndTime) < 7;
                }
            } catch (t) {
                o.e(t);
            } finally {
                o.f();
            }
            t.setData({
                couponList: e.data.canUseCouponList
            }), t.selectComponent("#ticket").showPopup(), wx.hideLoading();
        }).catch(function(t) {
            wx.hideLoading();
        });
    },
    getCouponHistoryId: function(t) {
        t.detail.couponHistoryId ? this.setData({
            discountType: 2,
            couponHistoryId: t.detail.couponHistoryId,
            integralStatus: null
        }) : this.setData({
            discountType: 0,
            couponHistoryId: ""
        }), this.getPrice();
    },
    getIntegral: function(t) {
        var e = t.detail.isUseIntegral;
        1 == e ? this.setData({
            integralStatus: e,
            discountType: e,
            couponHistoryId: ""
        }) : 0 == e && this.data.couponHistoryId ? this.setData({
            integralStatus: e
        }) : this.setData({
            integralStatus: e,
            discountType: e
        }), this.getPrice();
    },
    getCarProCount: function(t) {
        var e = this.data.validList, a = e.findIndex(function(e) {
            return e.productId == t.detail.product.productId;
        });
        e[a].quantity = t.detail.quantity, this.setData({
            validList: e,
            discountType: 0,
            couponHistoryId: ""
        }), this.setTitle(), t.detail.product.select && this.getPrice();
    },
    toIndex: function() {
        wx.switchTab({
            url: "/pages/index/index"
        });
    }
});